package ee.ut.math.chess.hadoop.dummy.fast;

import java.util.ArrayList;
import java.util.List;

import ee.ut.math.chess.base.model.Move;
import ee.ut.math.chess.base.model.chesspiece.ChessPiece;
import ee.ut.math.chess.base.model.chesspiece.Color;
import ee.ut.math.chess.base.model.exception.GameLogicException;
import ee.ut.math.chess.game.model.DummyPersistenceManager;
import ee.ut.math.chess.game.model.Game;

public class DummySkeletonFast {
    public Move getMove(Game game) {
        List<Move> moves = new ArrayList<Move>();
        for (int i=0; i < 8; i++) {
            for (int j=0; j <8; j++) {
                ChessPiece chessPiece = game.getBoard().getChessPiece(i, j);
                if (chessPiece != null && chessPiece.getColor() == Color.BLACK) {
                    List<Move> legalMoves = chessPiece.getLegalMoves(i, j, game, false);
                    for (Move move : legalMoves) {
                        try {
                            DummyPersistenceManager initial = new DummyPersistenceManager(game);
                            game.moveInternal(chessPiece, move);
                            initial.rollback();
                            moves.add(move);
                        } catch (GameLogicException e) {
                            continue;
                        }                        
                    }
                }
            }
        }
        return moves.get((int)(Math.random()*moves.size()));
    }
}
